<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/HumeurDatabase.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GameSession.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GaleryItem.interface.php");
	
	class Member implements GaleryItem{
		var $id;
		var $accountMail;
		var $nickname;
		var $birthDate;
		var $gender;
		var $savingResults;
		var $sessions;
		
		public function Member($data=0){
			$this->sessions = array();
			if(is_array($data)) $this->create($data);
			elseif(is_numeric($data)) $this->load($data);
		}
		
		public function load($id){
			$db = new HumeurDatabase();
			$data = mysql_fetch_assoc($db->query("SELECT * FROM members WHERE memberId = '".$id."' LIMIT 1;"));
			$this->create($data);
			$this->loadSessions();
		}
		
		public function loadSessions(){
			$this->sessions = array();
			$db = new HumeurDatabase();
			$this->members = array();
			$data = $db->query("SELECT gameSessionId FROM gameSessions WHERE memberId = '".$this->id."' ORDER BY gameSessionId ASC;");
			while($session = mysql_fetch_assoc($data)){
				$this->sessions[$session['gameSessionId']] = new GameSession($session['gameSessionId']);
			}
		}
		
		public function create($array){
			$this->id = (isset($array['memberId']))? $array['memberId']:NULL;
			$this->accountMail = (isset($array['accountMail']))? $array['accountMail']:NULL;
			$this->nickname = (isset($array['nickname']))? $array['nickname']:NULL;
			$this->birthDate = (isset($array['birthDate']))? $array['birthDate']:NULL;
			$this->gender = (isset($array['gender']))? $array['gender']:NULL;
			$this->savingResults = (isset($array['savingResults']))? $array['savingResults']:NULL;
		}
		
		public function save(){
			if($this->id == NULL){
				if(!$this->insert()) return false;
			}else{
				if(!$this->update()) return false;
			}
			if($this->savingResults == 1){
				foreach($this->sessions as $session){
					if(!$session->save()) return false;
				}
				$this->loadSessions();
			}
			return true;
		}
		
		public function insert(){
			$db = new HumeurDatabase();
			$result = $db->query("INSERT INTO members VALUES (NULL,'".$this->accountMail."','".$this->nickname."','".$this->birthDate."','".$this->gender."','".$this->savingResults."');");
			if($result){
				$this->id = mysql_insert_id();
			}
			return $result;
		}
		
		public function delete(){
			$db = new HumeurDatabase();
			$this->loadSessions();
			foreach($this->sessions as $session){
				$session->delete();	
			}
			return $db->query("DELETE FROM members WHERE memberId = '".$this->id."';");
		}
		
		public function update(){
			$db = new HumeurDatabase();
			return $db->query("UPDATE members SET nickname = '".$this->nickname."', birthDate = '".$this->birthDate."', gender = '".$this->gender."',savingResults = '".$this->savingResults."' WHERE memberId = '".$this->id."';");
		}
		
		public function summary(){
			return '<div class="leftFloat memberSummary '.(($this->gender == 1)?'male':'female').'" id="'.$this->id.'"><div class="memberInfos">'.$this->nickname.'<br/>'.(($this->gender == 1)?'Né le : ':'Née le : ').$this->birthDate.'<br/>'.(($this->savingResults == 1)?"Ses résultats sont sauvegardés":"Ses résultats ne sont pas sauvegardés").'</div>
			<a onclick="ajaxLoad(\'connectMember\',\'id='.$this->id.'\',jsonManage)"><img src="img/connectMember.png" alt="Connecter le membre"/></a>
			<a onclick="ajaxLoad(\'makeMemberEditForm\',\'id='.$this->id.'\',makeMemberEdit_response)"><img src="img/editMember.png" alt="Editer"/></a>
			<a onclick="if(confirm(\'Toutes les données seront supprimées. Êtes-vous sur de vouloir supprimer ce membre ?\')) ajaxLoad(\'deleteMember\',\'id='.$this->id.'\',deleteMember_response)"><img src="img/deleteMember.png" alt="Supprimer"/></a></div>';
		}
		
		public function addGameSession($session){
			$this->sessions[$session->id] = clone $session;
		}
		
		public function deleteGameSession($id){
			if($this->sessions[$id]->delete()){
				unset($this->sessions[$id]);
				return true;
			}
			return false;
		}
	}
?>